package com.controller;

import com.Util.DB;
import com.commont.BaiDuMapUtil;
import com.commont.IpUtil;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.jooq.DSLContext;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.sql.Connection;
import java.util.Map;

/**
 * 百度地图服务
 */
@Path ( "map" )
public class BaiDuMapController extends BaseController {

    /**
     * 根据地点搜索周边
     * @param query
     * @return
     */
    @POST
    @Path ( "get_location_search" )
    @Produces(MediaType.APPLICATION_JSON+";charset=utf-8")
    @Consumes(MediaType.APPLICATION_JSON)
    public String getSign(String query) {
        try (Connection conn = DB.getConnection ();
             DSLContext create = DSL.using ( conn, SQLDialect.MYSQL )) {
            //创建事务
            //解析参数
            this.jsonObject = new JsonParser ().parse ( query ).getAsJsonObject ().get ( "query" ).getAsJsonObject ();
            //获取userId
            //Integer userId = this.jsonObject.get ( "userId" ).getAsInt ();
            Integer currentPage = this.jsonObject.get ( "page" ).getAsInt ();
            Integer pageSize = this.jsonObject.get ( "pagenum" ).getAsInt ();
            String keyWord=this.jsonObject.get ( "keyWord" ).getAsString ();
            String address=this.jsonObject.get ( "address" ).getAsString ();
            //查出经纬度
            Map<String,Double> map= BaiDuMapUtil.getLan ( address );
            //获取数据
            JsonObject jsonObject=BaiDuMapUtil.getList ( keyWord,map.get ( "lat" ),map.get ( "lng" ),currentPage,pageSize );
            //JsonArray jsonArray=jsonObject.getAsJsonArray ();
            //log.info ( "【当前数据的数量】={}",jsonObject.size () );
            return success (jsonObject);
        } catch (Exception e) {
            return error ( -1,"异常" );
        }

    }
    /**
     * 地点搜索提醒
     * Place Suggestion API
     */
    @POST
    @Path ( "place_suggestion" )
    @Produces(MediaType.APPLICATION_JSON+";charset=utf-8")
    @Consumes(MediaType.APPLICATION_JSON)
    public String suggestion(String query) {
        try (Connection conn = DB.getConnection ();
             DSLContext create = DSL.using ( conn, SQLDialect.MYSQL )) {
            //创建事务
            //解析参数
            this.jsonObject = new JsonParser ().parse ( query ).getAsJsonObject ().get ( "query" ).getAsJsonObject ();
            String keyWord = this.jsonObject.get ( "keyWord" ).getAsString ();
            //先通过ip进行定位
            String ip=IpUtil.getIpAddr ( req );
            //获取地址
            String address=BaiDuMapUtil.getAddressByIp ( ip );
            //获取数据
            JsonObject jsonObject=BaiDuMapUtil.getListSuggestion ( keyWord,address );
            return success (jsonObject);
        } catch (Exception e) {
            return error ( -1, "异常" );
        }
    }
}